{% extends "base.html" %}
{% block body %}


<div id="main" role="main">

	<ol class="breadcrumb">
		<li><a href="{{ url_for('pic.home') }}">Home</a></li>
		<li><a href="{{ url_for('work.home') }}">Work</a></li>
		<li class="active">{{ album.label|title }}</li>
	</ol>

    <center>
        <div class="album-title">
            <form>
                <span class="pull-right">
                    <input type="button" class="btn btn-xs btn-default" value="刷新" id="refresh_btn">
                    <input type="button" class="btn btn-xs btn-default" value="选择图片" onclick="$('#input_file').click()" id="pics_select_count">
                    <input class="hidden" type="file" name="file" id="input_file" onchange="upload_files_count()" multiple="multiple" accept="image/gif,image/jpeg,image/jpg,image/png,image/webp,video/webm,video/mp4">
                    <input type="button" class="btn btn-xs btn-primary hidden" id="upload_files_btn" value="Upload">
                </span>
            </form>
            <h5>
                {{ album.label|title }}<br>
                <small> 图片数量: <span id="pics_amount">{{ album.amount }}</span> | 选中：<span id="chk_count">0</span></small><br>
            </h5>
            {{ macros.album_tools(album, siteinfo) }}
        </div>
    </center>

	<form id="batch_deal">
        <ul id="tiles">
            <!-- These are our grid blocks -->
            {%for pic in pics%}
            <li id="p_{{pic.valid_name}}">
                <a href="{{pic.url}}" target="_blank">
                    <img src="{{pic.url}}" width="200" />
                </a>
                {% if pic.album %}
                    <span class="temp_album_label">{{pic.album}}</span>
                {% endif %}
                <input type="checkbox" class="pics_chk" name="pics_chk" value="{{pic.file_name}}">
            </li>
            {% endfor %}
            <!-- End of grid blocks -->
        </ul>
	</form>
</div>
{% endblock %}


{% block script %}
<!----wookmark-scripts---->
<script type="text/javascript">
js_more_pics = {
        "page": parseInt("{{page | default(0, true)}}"),
        "url": "{{url_for('api.more_pic')}}",
        "label": "{{album.label}}",
        "ajax_post_data": {'source': "{{source}}"},
    };
load_more_pics()  // 加载第一页图片

$('#upload_files_btn').click(function (){
    let files = $('#input_file')[0].files,
        count = files.length,
        finish = 0,
        success = 0,
        error = 0,
        ignore = 0;
    $('#upload_files_btn').val('Upload 0/' + count);

    $.each(files, function (index, element) {
        let fd = new FormData();
        fd.append('file_size', element.size)
        fd.append('file', element);
        $.ajax({
            url: "{{url_for('api.upload_api')}}",
            type: "POST",
            async: true,
            cache: false,
            contentType: false,
            data: fd,
            dataType:"json",
            processData:false,
            success: function (result) {
                finish += 1
                $('#upload_files_btn').val('Upload ' + finish + '/' + count);
                if(result['status'] === 1){success += 1}
                else if(result['status']===0){error += 1}
                else {ignore += 1}
                if (finish===count){
                    alert('上传 ' + count + '/ 成功 ' + success + '/ 跳过 ' + ignore + '/失败 ' + error)
                    location.reload();
                }
            }
        });
    });
});


$('#refresh_btn').click(function(){
    let pics_list = [];
    $('#tiles').children().each(function(){pics_list.push($(this).attr('id'))});

    $.ajax({
        url: "{{url_for('api.temp_refresh')}}",
        type: 'POST',
        async:false,
        dataType:"json",
        contentType: "application/json",
        data: JSON.stringify({"pics": pics_list}),
        success: function (result) {
            $('#pics_amount').text(result['amount']);
            let done_list = result['done'];
            for (let id in done_list){
                $('#'+ done_list[id]).remove();
            }
            $('#tiles').trigger('refreshWookmark');
        }
    })
});


$('#sorted_import_btn').click(function () {
    let albums = $('#album_name').val(),
        tags = $('#tag_name').val(),
        volume = $('#volume_name').val(),
        actresses = $('#actress_name').val(),
        pics_list = batch_deal_checked();

    if (confirm('导入选中的图片？')===true) {
        $.ajax({
            url: "{{url_for('api.import_pics')}}",
            type: 'POST',
            async:false,
            dataType:"json",
            contentType: "application/json",
            data: JSON.stringify({"pics": pics_list, "albums":albums,"tags":tags,"volume": volume, "actresses": actresses}),
            success: function (result) {
                // alert(result['info']);
                $('#refresh_btn').click()
            }
        })
    }
});


$('#delete_temp_pics_btn').click(function () {
    let pics_list = batch_deal_checked();
    if (confirm('确定删除选中的图片吗？')===true) {
        $.ajax({
            url: "{{url_for('api.delete_temp_pic')}}",
            type: 'POST',
            async: false,
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify({"pics": pics_list}),
            success: function (result) {
                alert(result['info']);
                if (result['status'] === 1){ location.reload()}
            }
        })
    }
});


load_filter("", "{{url_for('api.load_filter', timestamp=siteinfo.cache_refresh_time('album'))}}")
 </script>
<!----//wookmark-scripts---->
{% endblock %}
